/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.score.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.service.CrudService;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.modules.personassessmentinfo.dao.PersonAssessmentInfoDao;
import com.fosung.frame.modules.score.dao.OrganizePersonDetailDao;
import com.fosung.frame.modules.score.entity.OrganizePersonDetail;

/**
 * 党员申请积分明细Service
 * 
 * @author ygl
 * @version 2017-09-03
 */
@Service
@Transactional(readOnly = true)
public class OrganizePersonDetailService extends CrudService<OrganizePersonDetailDao, OrganizePersonDetail> {

	@Autowired
	private PersonAssessmentInfoDao personAssessmentInfoDao;
	
	@Autowired
	private OrganizePersonDetailDao organizePersonDetailDao;
	
	
	public OrganizePersonDetail get(String id) {
		return super.get(id);
	}

	public List<OrganizePersonDetail> findList(OrganizePersonDetail organizePersonDetail) {
		return super.findList(organizePersonDetail);
	}

	public List<OrganizePersonDetail> findDistinctBYid(String id) {
		return organizePersonDetailDao.findDistinctBYid(id);
	}

	public Page<OrganizePersonDetail> findPage(Page<OrganizePersonDetail> page,
			OrganizePersonDetail organizePersonDetail) {
		return super.findPage(page, organizePersonDetail);
	}

	@Transactional(readOnly = false)
	public void save(OrganizePersonDetail organizePersonDetail) {
		super.save(organizePersonDetail);
	}

	@Transactional(readOnly = false)
	public void delete(OrganizePersonDetail organizePersonDetail) {
		super.delete(organizePersonDetail);
	}

	

	/**
	 * 校验党员积分是否超过年度上限（每一个积分内容）
	 * @param organizePersonDetail
	 * @param score
	 * @return
	 */
	public String isUpperPersonTotalScore(OrganizePersonDetail organizePersonDetail) {
		OrganizePersonDetail person = dao.findPersonTotalScore(organizePersonDetail);
		double totalScore = person==null ? 0 : Math.abs(Double.parseDouble(person.getTotalScore()));
		double upperScore = Double.parseDouble(personAssessmentInfoDao.get(organizePersonDetail.getPersonAssessmentInfoId()).getUpperScore());
		double applyScore = Double.parseDouble(organizePersonDetail.getApplyScore());
		String type = organizePersonDetail.getType();
		double score = 0;
		if("1".equals(type)) {
			score = totalScore + applyScore;
		}else if("2".equals(type)){
			score = Math.abs(totalScore - applyScore);
		}
		if(score > upperScore) {
			return "1";
		}else {
			return "0";
		}
	}
	
	/**
	 * 针对党员积分审批 每个党员审核积分可以自设
	 * @param organizePersonDetail
	 * @return
	 */
	public String isUpperPersonTotalScore1(OrganizePersonDetail organizePersonDetail) {
		OrganizePersonDetail person = dao.findPersonTotalScore(organizePersonDetail);
		double totalScore = person==null ? 0 : Math.abs(Double.parseDouble(person.getTotalScore()));
		if(!StringUtils.isEmpty(organizePersonDetail.getIsBatch()) && "1".equals(organizePersonDetail.getIsBatch())) {
			OrganizePersonDetail obj = dao.findPersonNoShTotalScore(organizePersonDetail);
			double noShScore = Math.abs(Double.parseDouble(obj.getTotalScore()));
			totalScore = totalScore + noShScore;
		}
		
		double upperScore = Double.parseDouble(personAssessmentInfoDao.get(organizePersonDetail.getPersonAssessmentInfoId()).getUpperScore());
		double applyScore = Double.parseDouble(organizePersonDetail.getCheckScore());
		String type = organizePersonDetail.getType();
		double score = 0;
		if("1".equals(type)) {
			score = totalScore + applyScore;
		}else if("2".equals(type)){
			score = Math.abs(totalScore - applyScore);
		}
		if(score > upperScore) {
			return "1";
		}else {
			return "0";
		}
	}

	public void updateStatusAndScore(OrganizePersonDetail organizePersonDetail) {
		dao.updateStatusAndScore(organizePersonDetail);
	}
	

	/**
	 * 批量同意积分审批
	 * @param organizeLifeId
	 */
	public void batchAgreePersonScoreDetail(String organizeLifeId) {
		dao.batchAgreePersonScoreDetail(organizeLifeId);
	}

	public String findPersonTotal(String id) {
		return dao.findPersonTotal(id);
	}

}